package com.uchengwang.ui.cm_ui.login.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.cm_ui.login.adapter.IntroduceAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/9.
 * 启动介绍页
 */

public class IntroduceActivity extends AllBaseActivity{
    private ViewPager viewPager;
    private int[] res = new int[]{R.drawable.introduce1, R.drawable.introduce2, R.drawable.introduce3, R.drawable.introduce4, R.drawable.introduce5};
    private List<View> viewList = new ArrayList<>();
    private IntroduceAdapter adapter;
    private float startX, endX;
    private int selectIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //隐藏状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_introduce);

        initView();
    }

    private void initView(){
        viewPager = (ViewPager) findViewById(R.id.introduce_vp);

        initData();
    }

    private void initData(){
        for (int i =0; i<res.length; i++){
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageManager.getInstance().loadImage(this, imageView, res[i], ImageManager.ImageType.NARMOL);
            viewList.add(imageView);
        }
//
//        View view = LayoutInflater.from(this).inflate(R.layout.introduce_layout, null, false);
//        ImageView imageView = (ImageView) view.findViewById(R.id.introduce_iamge);
//        ImageManager.getInstance().loadImage(this, imageView, R.drawable.main_image_s, ImageManager.ImageType.NARMOL);
//
//        Button bt = (Button) view.findViewById(R.id.introduce_bt);
//        bt.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivity(new Intent(IntroduceActivity.this, LoginActivity.class));
//                IntroduceActivity.this.finish();
//            }
//        });
//        viewList.add(view);

        adapter = new IntroduceAdapter(viewList);
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                Log.d("jjf", "onPageSelected   position=="+position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        break;

                        case MotionEvent.ACTION_UP:
                            endX = event.getX();

                            if (selectIndex ==(res.length-1) && (startX -endX)>200 ){
                                startActivity(new Intent(IntroduceActivity.this, LoginActivity.class));
                                IntroduceActivity.this.finish();
                            }

                            Log.d("jjf", "startX=="+startX+"==endX=="+endX);

                            break;
                }
                return false;
            }
        });

    }

    private BitmapDrawable getBitmap(int resId){
        BitmapFactory.Options opt = new BitmapFactory.Options();

        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = getResources().openRawResource(resId);

        Bitmap bitmap = BitmapFactory.decodeStream(is,null, opt);

        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new BitmapDrawable(getResources(),bitmap);
    }

}
